

function validateAddLeave(){

	var dateOfFiling = document.getElementById("dateOfFiling").value;
	var dateReceived = document.getElementById("dateReceived").value;
	var typeOfLeave = document.getElementById("typeOfLeave").value;
	var days = document.getElementById("days").value;
	var startDate = document.getElementById("startDate").value;
	var endDate = document.getElementById("endDate").value;
	var status = document.getElementById("status").value;
	var errorMessage = "";
	var errorFlag = 0;
	var result = "";
	var dateOfFilingFlag = false, dateReceivedFlag = false;
	var startDateFlag = false, endDateFlag = false, daysFlag=false;			
	var	daysComputed = 0;
	var daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	
	//validate type of leave
	if(typeOfLeave == "null")
	{
		errorMessage += "No type of leave selected.\n";
		errorFlag = 1;
		document.getElementById("typeOfLeave").style.backgroundColor="#FFFF99";
	} else {
		document.getElementById("typeOfLeave").style.backgroundColor="white";
	}
	
	//validate days
	if(days.length == 0){
		
		errorMessage += "Days empty.\n";
		errorFlag = 1;
		daysFlag = false;
		document.getElementById("days").style.backgroundColor="#FFFF99";
		
	} else {
		daysFlag = true;
		document.getElementById("days").style.backgroundColor="white";
	}
	
	//validate start date
	if(checkFormat(startDate)==false) {
	   
		errorMessage += "Incorrect start date\n";
		errorFlag = 1;
		startDateFlag = false;
		document.getElementById("startDate").style.backgroundColor="#FFFF99";
		
	} else {
		
		//call function to check date
		result = checkDate(startDate.substring(0,2),
				  startDate.substring(3,5),
				  startDate.substring(6,10));
								  
		if ( result==false ) {
			errorMessage+="Incorrect start date\n";
			errorFlag = 1;
			startDateFlag = false;
			document.getElementById("startDate").style.backgroundColor="#FFFF99";
		} else {
			startDateFlag = true;
			document.getElementById("startDate").style.backgroundColor="white";
		}
	}
	
	//validate end date
	if(checkFormat(endDate)==false) {
	   
		errorMessage += "Incorrect end date\n";
		errorFlag = 1;
		endDateFlag = false;
		document.getElementById("endDate").style.backgroundColor="#FFFF99";
	} else {
		
		//call function to check date
		result = checkDate(endDate.substring(0,2),
				  endDate.substring(3,5),
				  endDate.substring(6,10));
				  
		if ( result==false ) {
			errorMessage += "Incorrect end date\n";
			errorFlag = 1;
			endDateFlag = false;
			document.getElementById("endDate").style.backgroundColor="#FFFF99";
		} else {
			endDateFlag = true;
			document.getElementById("endDate").style.backgroundColor="white";
		}
	}
	
	if(status == "null"){
		errorMessage += "No type of status selected.\n";
		errorFlag = 1;
		document.getElementById("status").style.backgroundColor="#FFFF99";
	} else {
		document.getElementById("status").style.backgroundColor="white";
	}
	
	if (startDateFlag==true && endDateFlag==true && daysFlag==true) {
								
		//if the year in end date is earlier than start date
		if (endDate.substring(6,10) <
					startDate.substring(6,10)) {
					errorMessage += "Incorrect end date.\n";
					errorFlag = 1;
					document.getElementById("endDate").style.backgroundColor="#FFFF99";
		} else {
			if (endDate.substring(0,2) < 
				startDate.substring(0,2)) {
				errorMessage += "Incorrect end date.\n";
				errorFlag = 1;
				document.getElementById("endDate").style.backgroundColor="#FFFF99";
			} else if (endDate.substring(0,2) ==
					   startDate.substring(0,2)){
				if (endDate.substring(3,5) < startDate.substring(3,5)) {
					errorMessage += "Incorrect end date.\n";
					errorFlag = 1;
					document.getElementById("endDate").style.backgroundColor="#FFFF99";
				} else {
					document.getElementById("endDate").style.backgroundColor="white";
				}
			} else {
				document.getElementById("endDate").style.backgroundColor="white";
			}
		} 
		
															
		//checks if the months are equal
		if (startDate.substring(0,2)==endDate.substring(0,2)) {
							
			daysComputed = (parseInt(endDate.substring(3,5), 10)-
							parseInt(startDate.substring(3,5), 10))+1;
		
							
		}
		//if there is one month difference
		else if (startDate.substring(0,2)-endDate.substring(0,2)==1 || 
				 startDate.substring(0,2)-endDate.substring(0,2)==-1) {
			
			if(daysInMonth[startDate.substring(0,2)-1] == 31) {
				daysComputed = parseInt(31 - startDate.substring(3,5), 10)+
							   parseInt(endDate.substring(3,5), 10)+1;
			} else if (daysInMonth[startDate.substring(0,2)-1] == 30){
				daysComputed = parseInt(30 - startDate.substring(3,5), 10)+
							   parseInt(endDate.substring(3,5), 10)+1;
			} else if (daysInMonth[startDate.substring(0,2)-1] == 28){
				daysComputed = parseInt(28 - startDate.substring(3,5), 10)+
							   parseInt(endDate.substring(3,5), 10)+1;
			}
		
		}
		//if the month has more than one difference
		else {
			errorMessage += "Days exceed maximum number.\n";
			errorFlag = 1;
			document.getElementById("days").style.backgroundColor="#FFFF99";
		}
		
		
		//Checks if the start date is december, and end date is january
		if (parseInt(endDate.substring(6,10), 10)-
			parseInt(startDate.substring(6,10), 10) == 1) {
			
			if (startDate.substring(0,2) == 12 && 
				endDate.substring(0,2) == 1) {
				daysComputed = (31-parseInt(startDate.substring(3,5), 10))+
								parseInt(endDate.substring(3,5), 10) + 1;
				
			} else {
				errorMessage += "Incorrect number of days\n";
				errorFlag = 1;
				document.getElementById("days").style.backgroundColor="#FFFF99";
			}
		} 
		
		//if the number of days computed is not equal to the number of days specified
		if (daysComputed != days) {
			errorMessage += "Incorrect number of days\n";
			errorFlag = 1;
			document.getElementById("days").style.backgroundColor="#FFFF99";
		}
		
	}
	
	//summary of errors
	if( errorFlag == 1) {
		alert(errorMessage);				
		return false;
	} else {
		return true;
	}											
}

function checkFormat (date) {

	if (date.length == 0 || date.length != 10 || 
	   date.charAt(2)!='/' || date.charAt(5)!='/' ||
	   isNaN(date.substring(0,2)) || 
	   isNaN(date.substring(3,5)) ||
	   isNaN(date.substring(6,10))) {
		return false;
	   }

	return true;

}

function checkDate ( month, day, year ) {

	var errorFlag = 0;
	year = parseInt(year, 10);
	
	if (day>=1 && month>=1 &&  month<=12 && year>=2000) {
						
		//months with 31 days
		if (month==1 || month==3 || month == 5 || month==7 || 
			 month==8 || month==10 || month == 12) {
			
			if ( day>31 ) {
				errorFlag = 1;
			}
			
		} else {
			
			if ( month == 2 ) {
				if ( year%4 == 0) { 
					if ( day>29 ) {
						errorFlag = 1;
					}
				} else {
					if ( day>28) {
						errorFlag = 1;
					}
				}							
			} else {
				if ( day>30 ) {
					errorFlag = 1;
				}							
			}						
		}
	
	} else {					
		errorFlag = 1;
	}
	
	if ( errorFlag==1 ) {
		return false;
	} else {
		return true;
	}

}
